
/****************************************************SETTING DASHBOARD******************************************************************/
var memberList = Object();
var lastSprint = Object();


function addNewIssue(issue1){
	var issue = $('<div class="span4 sticker sticker-medium" data-toggle="context" data-target="#context-menu" id="'+issue1.id+'">'+
				'<div class="sticker-header">'+
				'<div class="row">'+
				'<div class="header-left span3 pull-left">'+
				'<div class="small-lead">#'+issue1.id+' '+issue1.subject+'</div>'+
				'</div>'+
				'<div class="header-right pull-right">'+
		
				'<img title="TEST" class="img-circle avatar" data-toggle="dropdown" data-target="#assign-menu"  src="../avatarImages/user.png" alt="" />'+
		
				'</div>'+				
				'</div>'+
				'<div class="task-progress" style="width: 80%;"></div>'+
				'</div>'+
				'<div class="sticker-inner">'+
				'<div>'+issue1.description+'</div>'+
				'</div>'+
				'<div class="sticker-footer">'+
				'<div class="row">'+
				'<div class="span2">V: 30 | Ri: 40 | R: '+issue1.remain+'</div>'+
				'<ul class="icon-wrapper unstyled inline pull-right text-right">'+
				'<li><a href="#"><i class="icon-comments"></i></a></li>'+
				'<li><a href="#"><i class="icon-pencil"></i></a></li>'+
				'<li><a href="#"><i class="icon-remove"></i></a></li>'+
				'<li><a href="#"><i class="icon-plus"></i></a></li>'+
				'</ul>'+
				'</div>'+				
				'</div>'+
				'<div class="comment-wrapper">'+
				'<div class="comment-input-wrapper row">'+
				'<img class="span1 img-circle avatar" src="../avatarImages/user.png", absolute : true)}" alt="" />'+
				'<div class="span3">'+
				'<textarea class="input-block-level" placeholder="Enter Your Comment"></textarea>'+
				'</div>'+
				'</div>'+
				'<div class="comment row">'+
				'<img class="span1 img-circle avatar" src="../avatarImages/user.png", absolute : true)}" alt="" />'+
				'<div class="span3 comment-content">'+
				'<p class="comment-time small-lead">Said at 16:00PM</p>'+
				'<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr,'+
				'sed diam nonumy eirmod tempor invidunt ut labore et dolore'+
				'magna aliquyam erat, sed diam voluptua.</p>'+
				'</div>'+
				'</div>'+
				'<div class="comment row">'+
				'<img class="span1 img-circle avatar input-block-level"'+
				'src="../avatarImages/user.png", absolute : true)}" alt="qvinh" />'+
				'<div class="span3 comment-content">'+
				'<p class="comment-time small-lead">Said at 17:00PM</p>'+
				'<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr,'+
				'sed diam nonumy eirmod tempor invidunt ut labore et dolore'+
				'magna aliquyam erat, sed diam voluptua.</p>'+
				'</div>'+
				'</div>'+
				'</div>'+
				'</div>');
		issue.hide();
		$('td[statusid='+issue1.status.id+']').prepend(issue);
		issue.fadeIn();
		issue.effect("pulsate", { times:4 }, 5000);
}




function appendTask(index,issue1,memberAssigned){
	var issue = '<div class="span4 sticker sticker-medium" data-toggle="context" data-target="#context-menu" id="'+issue1.id+'">'+
				'<div class="sticker-header">'+
				'<div class="row">'+
				'<div class="header-left span3 pull-left">'
		issue+=	'<div class="small-lead">#'+issue1.id+' '+issue1.subject+'</div>'
		issue+=	'</div>'+
				'<div class="header-right pull-right">'
		if(issue1.assigned!=null){
			issue+=	' <img title="TEST" class="img-circle avatar" data-toggle="dropdown" data-target="#assign-menu"  src="../avatarImages/'+memberAssigned[''+issue1.id+''].avatar+'" alt="" />'
		}else{
			issue+=	' <img title="TEST" class="img-circle avatar" data-toggle="dropdown" data-target="#assign-menu"  src="../avatarImages/user.png" alt="" />'
		}
		issue+=	'</div>'+				
				'</div>'+
				'<div class="task-progress" style="width: 80%;"></div>'+
				'</div>'+
				'<div class="sticker-inner">'
		issue+=	'<div>'+issue1.description+'</div>'
		issue+= '</div>'+
				'<div class="sticker-footer">'+
				'<div class="row">'
		issue+=	'<div class="span2">V: 30 | Ri: 40 | R: '+issue1.remain+'</div>'+
				'<ul class="icon-wrapper unstyled inline pull-right text-right">'+
				'<li><a href="#"><i class="icon-comments"></i></a></li>'+
				'<li><a href="#"><i class="icon-pencil"></i></a></li>'+
				'<li><a href="#"><i class="icon-remove"></i></a></li>'+
				'<li><a href="#"><i class="icon-plus"></i></a></li>'+
				'</ul>'+
				'</div>'
				
		issue+=	'</div>'+
				'<div class="comment-wrapper">'+
				'<div class="comment-input-wrapper row">'+
				'<img class="span1 img-circle avatar" src="../avatarImages/user.png", absolute : true)}" alt="" />'+
				'<div class="span3">'+
				'<textarea class="input-block-level" placeholder="Enter Your Comment"></textarea>'+
				'</div>'+
				'</div>'
			
		issue+= '<div class="comment row">'+
				'<img class="span1 img-circle avatar" src="../avatarImages/user.png", absolute : true)}" alt="" />'+
				'<div class="span3 comment-content">'+
				'<p class="comment-time small-lead">Said at 16:00PM</p>'+
				'<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr,'+
				'sed diam nonumy eirmod tempor invidunt ut labore et dolore'+
				'magna aliquyam erat, sed diam voluptua.</p>'+
				'</div>'+
				'</div>'+
				'<div class="comment row">'+
				'<img class="span1 img-circle avatar input-block-level"'+
				'src="../avatarImages/user.png", absolute : true)}" alt="qvinh" />'+
				'<div class="span3 comment-content">'+
				'<p class="comment-time small-lead">Said at 17:00PM</p>'+
				'<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr,'+
				'sed diam nonumy eirmod tempor invidunt ut labore et dolore'+
				'magna aliquyam erat, sed diam voluptua.</p>'+
				'</div>'+
				'</div>'+
				'</div>'+
				'</div>'
				
		$('td[statusid='+issue1.status.id+']').append(issue);	
}

			
function changeTeam(id,url) {
	if(id){
		$.ajax({
			url: url,
			async: false,
			cache: false,
			dataType: 'json',
			data: {
				teamid: id
			},
			beforeSend:function(){
				$('div#kanban').block({
					centerY: 0,
					css: {'top': '100px'},
					message: '<h4><img src="../images/skin/ajax-loader.gif" /> Loading...</h4>'
		        }); 
			},
			success: function(data){
				$("select#sprint").empty();
				for(var i = 0; i < data.sprintList.length;i++){
					$('<option value='+data.sprintList[i].id+'>'+data.sprintList[i].sprintName+'</oprion>').appendTo($('select#sprint'));
				}
				
				//Select last sprint of team
				if(data.lastSprint[0]){
					lastSprint=data.lastSprint[0];
					$("select#sprint").val(lastSprint[0]).attr('selected',true);
				}
				
//				loadKanban(id,lastSprint[0]);
				
				//Set up sub assign menu
				setSubAssignMenu(data.memberList);				
				
				//Set memberList
				memberList = data.memberList;			
			}
		});
		
	}else{
		$("select#sprint").empty();
	}
	
}

//Set assign menu
function setSubAssignMenu(data){
	//Remove all item
	$('#assign-menu .dropdown-menu').find('li').each(function(){$(this).remove()})
	//Set new item
	$('#assign-menu .dropdown-menu').append('<li><a id="null" tabindex="-1" href="#"> <img class="img-circle avatar"'+ 
											'src="../avatarImages/user.png" alt="none" />None</a></li>');
	$.each(data,function(index,obj){
		var img = $('<li><a id="'+obj[0].id+'" tabindex="-1" href="#">'+
					'<img class="img-circle avatar" src="../avatarImages/'+obj[0].avatar+'" alt="'+obj[0].lastName+'" />'+
					' '+obj[0].firstName+' '+obj[0].lastName+'</a></li>');
		$('#assign-menu .dropdown-menu').append(img);
	});
}

//Set context menu
function setContextMenu(data){
	$('#context-menu .assign .dropdown-menu').find('li').each(function(){$(this).remove()})
	//Set new item
	$('#context-menu .assign .dropdown-menu').append('<li><a id="null" tabindex="-1" href="#"> <img class="img-circle avatar"'+ 
											'src="../avatarImages/user.png" alt="none" />None</a></li>');
	$.each(data,function(index,obj){
		var img = $('<li><a id="'+obj[0].id+'" tabindex="-1" href="#">'+
					'<img class="img-circle avatar" src="../avatarImages/'+obj[0].avatar+'" alt="'+obj[0].lastName+'" />'+
					' '+obj[0].firstName+' '+obj[0].lastName+'</a></li>');
		$('#context-menu .assign .dropdown-menu').append(img);
	});
}

//Load kanban
function loadKanban(teamid,sprintid){
	$.ajax({
		type: 'GET',
		url: GET_KANBAN,
		cache: false,
		data:{ teamid: teamid, sprintid : sprintid },		
		beforeSend:function(){
			$('div#kanban').block({
				centerY: 0,
				css: {'top': '100px'},
				message: '<h4><img src="../images/skin/ajax-loader.gif" /> Loading...</h4>'
	        }); 
		},
		success: function(data){
			$('div#kanban').unblock();
			$('#kanban').html(data).hide().fadeIn();
		}
	});
}

//Load issue
function findIssueBySprint(sprintid){
	clearAllTask();
	$.get(GET_ISSUE_FOR_KANBAN,{sprintID: sprintid},function(data){
			if(data.issueList){
				$.each(data.issueList, function(index,issue){ appendTask(index,issue,data.memberAssigned)});
			}
		  },'json');
}

//Resize kanban
function resizeKanban(){
	var length = $('.board').find('th').length;
	var windowWidth = $(window).width();
	$('.board').find('th:not(".expand")').each(function(){
		$(this).css({'width': 100/length+'%'});
	})
}

//Delete all task
function clearAllTask(){
	$('.board').find('td').each(function(){
		$(this).children().remove();
	})
}

//SetupDraggable
function setupDraggable() {
	$(".task-group .sticker").draggable("destroy"); // Destroy draggable function
	// Jquery UI processing
	$(".task-group .sticker").draggable({
		connectToSortable : '.task-group',
		snap: true
	}).disableSelection();

	$(".task-group").sortable({
		connectWith: ".task-group",
//		forcePlaceholderSize: true,
		revert : 200,
		items : '> .sticker',
		opacity: 0.8,
//		placeholder: "ui-sortable-placeholder",
		start: function(event, ui){
            $(ui.item).width($('.task-group .sticker').width());
        },
        receive : function(event, ui) {
        	
			var sprintID = $("#sprint-select").val();
			if(sprintID != "-1") {
				var issueID = $(ui.item).attr("id");
				var statusID = $(this).attr('statusid');
				$.post(UPDATE_STATUS_ISSUE,{issueid : issueID, statusid : statusID})				
			}
		}
		}).disableSelection();
}

/****************************************************ADD NEW ISSUE FORM******************************************************************/
function setForm(){
	getProject();
	setMemberSelect(memberList);
	setLastSprint(lastSprint);
	getStatus();
}
function getProject(){
	$("#proselect").empty();
	$.ajax({
	        url: GET_PROJECT_BY_USER,
	        cache: false,
	        type: 'post',
	        dataType: 'json',
	        success: function (data) {
	            if (data.project) {
	                $.each(data.project, function (id, project) {
	                    $("#proselect").append('<option value="' + project.id + '">' + project.projectName + '</option>');
	                });
	            }
	            if (data.pro) {
	                $.each(data.pro, function (id, pro) {
	                    $("#proselect").append('<option value="' + pro[4].id + '">' + pro[5] + '</option>');
	                });
	            }
	            getUserStory();
	        }
	    });
}

function setMemberSelect(data){
	 $("#assigned").empty();
	 $("#assigned").append('<option value="null"> - </option>');
	 $.each(data,function(index,obj){
		 $("#assigned").append('<option value="' + obj[0].id + '">'+obj[0].firstName+' '+obj[0].lastName+'</option>');
	 })
}

function setLastSprint(data){
	$('input#sprint').attr('value',data[1]);
	$('input#sprint').attr('sprintid',data[0]);
}

function getUserStory(){
	var proId = ($("#proselect").val());
	 $("#uSselect").empty();
	$.ajax({
        url: GET_PROJECT_BY_PROJECT,
//        cache: false,
        type: 'post',
        dataType: 'json',
        data: {proId : proId},
        success: function (data) {            
            if (data.us) {
	                $.each(data.us, function (id, ust) {
	                    $("#uSselect").append('<option value="' + ust[0] + '">' + ust[1] + '</option>');
	                });
	            }
           }
                });
            

        }

function getStatus(){
	   var teamId = ($("#team").val());
	   var sprintId = $("#sprint").val();
	   $("select#status").empty();
	   $.ajax({
	        url: GET_STATUS,
//	        cache: false,
	        type: 'post',
	        dataType: 'json',
	        data: {teamId : teamId, sprintId : sprintId},
	        success: function (data) {
	        	if (data.status) {
	                $.each(data.status, function (id, status) {
	                    $("select#status").append('<option value="' + status[0] + '">' + status[1] + '</option>');
	                });
	            }   			                		               
		            }
	           });
	   }

//
function is_int(value){
	  if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
	      return true;
	  } else {
	      return false;
	  }
	}

//
 function testDT(str){
	    var a = str.split('');
		var flag=true;
		var i=1;
	   	if(a[0]=='D')
	   	{
			for(; i<= a.indexOf('T')-1; i++)
			{					
				if(!is_int(a[i]))
					flag=false;
			}
		}
		if(a[i]=='T')
		{
			i++;
			for(; i< str.length; i++)
			{											
				if(!is_int(a[i]))
					flag=false;
			}
		}
	   	else 
		   	flag=false;
	   	
	   	return flag;	
	   }

 //
function checkOnAddIssueForm(){		   	
		var remain = $("#remain").val().trim();
		var estimate = $("#estimate").val().trim();
		var testR = testDT(remain);
		var testE = testDT(estimate);
		var us = $("#uSselect").val();
		if (testR==false){
			$("#s1").show();
			return false;
		}
		else if(testE==false){
				$("#s2").show();						
				return false;
		}				
		else {
			$("#s1").hide();
			$("#s2").hide();					
		}
		if(us==null){
			$("#s3").show();
			return false;	
		}
		else {
				$("#s3").hide();
		}
 }



